#include "mydialog.h"
#include <QListWidget>
#include "Myprecent.h"
#include "mybutton/mybuttonwidget.h"
#include "mywebbrowser/browserwindow.h"
#include "mytableview/mytableviewwidget.h"
#include "toolswidget.h"
#include "mytreeview.h"
MyDialog::MyDialog(QWidget *parent,  Qt::WindowFlags f)
    : QWidget(parent, f)
    , m_pMyTreeView(nullptr)
{
    m_pMainLayout = new QHBoxLayout();
    m_pMainLayout->setContentsMargins(5,5,5,5);

    m_pMyTreeView = new MyTreeView(this);
    connect(m_pMyTreeView, SIGNAL(clicked(QModelIndex)), this, SLOT(onItemClicked(QModelIndex)));

    m_pMyTreeView->setBackgroundColor("");
    stIemData data1;
    data1.m_qsImageUrl = ":/qunyoujian.png";
    data1.m_qsHilightUrl = ":/qunyoujian-2.png";
    data1.m_qsMessage = "百分比";
    data1.m_qsDataValue = "Myprecent";
    m_pMyTreeView->addItem(data1);

    data1.m_qsImageUrl = ":/dakaiwenjian.png";
    data1.m_qsHilightUrl = ":/dakaiwenjian-2.png";
    data1.m_qsMessage = "按钮组控件";
    data1.m_qsDataValue = "mybuttonwidget";
    m_pMyTreeView->addItem(data1);

    data1.m_qsImageUrl = ":/qunyoujian.png";
    data1.m_qsHilightUrl = ":/qunyoujian-2.png";
    data1.m_qsMessage = "浏览器";
    data1.m_qsDataValue = "browserwindow";
    m_pMyTreeView->addItem(data1);

    data1.m_qsImageUrl = ":/qunyoujian.png";
    data1.m_qsHilightUrl = ":/qunyoujian-2.png";
    data1.m_qsMessage = "ToolsWidget";
    data1.m_qsDataValue = "ToolsWidget";
    m_pMyTreeView->addItem(data1);

    m_pMyTreeView->setFixedWidth(120);
    m_pMyTreeView->expandAll();
    m_pMyTreeView->show();

    m_pMainLayout->addWidget(m_pMyTreeView);
    m_pMainLayout->setAlignment(m_pMyTreeView, Qt::AlignLeft);
    //m_pMainLayout->addStretch();
    // Myprecent* myprecent = new Myprecent(this);
    // myprecent->setMinimumSize(200, 200);
    // m_pMainLayout->addWidget(myprecent);
    this->setLayout(m_pMainLayout);
    setMinimumSize(800, 600);
}

void MyDialog::onItemClicked(QModelIndex index)
{
   for(QMap<QString, QWidget*>::iterator iter = m_mapWidget.begin(); iter !=
         m_mapWidget.end(); iter++)
   {
        iter.value()->hide();
   }

   QStandardItem* pItem = m_pMyTreeView->getItem(index);
   if(pItem != nullptr)
   {
        stIemData retrievedData = pItem->data(IdRole).value<stIemData>();
        QString strVaule = retrievedData.m_qsDataValue;
        if(strVaule == "Myprecent")
        {
            if(!m_mapWidget.contains("Myprecent"))
            {
                Myprecent* myprecent = new Myprecent(this);
                myprecent->setMinimumSize(200, 200);
                m_pMainLayout->addWidget(myprecent);
                m_mapWidget.insert("Myprecent", myprecent);
                myprecent->show();
                //m_pMainLayout->
            }
            else
            {
                m_mapWidget["Myprecent"]->show();
            }
        }
        else if(strVaule == "mybuttonwidget")
        {
            if(!m_mapWidget.contains("mybuttonwidget"))
            {
                MyButtonWidget* pWidget = new MyButtonWidget(this);
                m_mapWidget.insert("mybuttonwidget", pWidget);
                m_pMainLayout->addWidget(pWidget);
            }
            else
            {
                m_mapWidget["mybuttonwidget"]->show();
            }

        }
        else if(strVaule == "browserwindow")
        {
            MytableViewWidget* pWidget = new MytableViewWidget(this);
            // BrowserMainWindow* pwidget = new BrowserMainWindow(this);
            // pwidget->show();
            m_pMainLayout->addWidget(pWidget);
        }
        else if(strVaule == "ToolsWidget")
        {
            if(!m_mapWidget.contains("ToolsWidget"))
            {
                ToolsWidget* pWidget = new ToolsWidget(this);
                pWidget->setFixedHeight(80);
                pWidget->addIconBtn(":/qunyoujian.png");
                pWidget->addIconBtn(":/qunyoujian.png");
                pWidget->addIconBtn(":/qunyoujian.png");
                pWidget->addIconBtn(":/qunyoujian.png");
                // pWidget->addIconBtn("");
                // pWidget->addIconBtn("");
                // pWidget->addIconBtn("");
                // pWidget->addIconBtn("");
                // pWidget->addIconBtn("");
                pWidget->setFixedWidth(this->width() - m_pMyTreeView->width());
                pWidget->show();
                m_pMainLayout->addWidget(pWidget);
                m_mapWidget.insert("ToolsWidget", pWidget);
                m_mapWidget["ToolsWidget"]->show();
            }
            else
            {
                m_mapWidget["ToolsWidget"]->show();
            }
        }
   }
}


